package gwtadmin.client;

//import java.util.ArrayList;
import gwtadmin.client.wsdltree.QName;
import gwtadmin.client.wsdltree.UniqList;
import gwtadmin.client.wsdltree.WtBinding;
import gwtadmin.client.wsdltree.WtMessage;
import gwtadmin.client.wsdltree.WtOperation;
import gwtadmin.client.wsdltree.WtPolicy;
import gwtadmin.client.wsdltree.WtPort;
import gwtadmin.client.wsdltree.WtPortType;
import gwtadmin.client.wsdltree.WtService;

import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;


public class WsdlTreeInfo implements IsSerializable {

	public int getServicesCount() {
		return services.size();
	}

	public int getPortTypesCount() {
		return portTypes.size();
	}

	public WtService getService(QName qname) {
		return UniqList.get(qname, services);
	}
	
	public WtPortType getPortType(QName qname) {
		return UniqList.get(qname, portTypes);
	}
	
	public WtBinding getBinding(QName qname) {
		return UniqList.get(qname, bindings);
	}
	
	public WtPort getPort(QName qname) {
		return UniqList.get(qname, ports);
	}
	
	public WtOperation getOperation(QName qname) {
		return UniqList.get(qname, operations);
	}
	
	public WtMessage getMessage(QName qname) {
		return UniqList.get(qname, messages);
	}
	
	public WtPolicy getPolicy(QName qname) {
		return UniqList.get(qname, policies);
	}
	
	public List<WtBinding> findBindings(QName portQName) {
		List<WtBinding> list = new UniqList<WtBinding>();

		for (WtBinding bind : bindings) {
			if (bind.portType.qname.equals(portQName))
				list.add(bind);
		}
		
		return list;
	}
	
	public List<WtBinding> findBindings(WtPortType portType) {
		return findBindings(portType.qname);
	}
	
	public List<WtService> findServices(QName portTypeQName) {
		List<WtService> list = new UniqList<WtService>();

		for (WtService svc : services) {
			for (WtPort port : svc.ports) {
				if (portTypeQName.equals(port.binding.portType.qname))
					list.add(svc);
			}
		}
		
		return list;
	}
	
	public List<WtService> findServices(WtPortType portType) {
		return findServices(portType.qname);
	}
	
	public boolean addService(WtService service) {
		if (!services.add(service))
			return false;
		
		for (WtPort port : service.ports)
			addPort(port);
		
		for (WtPolicy pol : service.policies)
			addPolicy(pol);
		
		return true;
	}
	
	public boolean addPortType(WtPortType portType) {
		if (!portTypes.add(portType))
			return false;
		
		for (WtOperation op : portType.operations)
			addOperation(op);
		
		return true;
	}
	
	public boolean addBinding(WtBinding binding) {
		if (!bindings.add(binding))
			return false;
		
		addPortType(binding.portType);
		return true;
	}
	
	private boolean addMessage(WtMessage msg) {
		return messages.add(msg);
	}
	
	private boolean addOperation(WtOperation op) {
		if (!operations.add(op))
			return false;
		
		addMessage(op.msgInput);
		addMessage(op.msgOutput);
		
		if (op.msgFaults != null)
			for (WtMessage msg : op.msgFaults)
				addMessage(msg);
		
		return true;
	}
	
	private boolean addPort(WtPort port) {
		if (!ports.add(port))
			return false;
		
		addBinding(port.binding);
		return true;
	}
	
	private boolean addPolicy(WtPolicy policy) {
		return policies.add(policy);
	}

	public List<WtService> services = new UniqList<WtService>();
	public List<WtPortType> portTypes = new UniqList<WtPortType>();	
	public List<WtBinding> bindings = new UniqList<WtBinding>();
	
	public List<WtMessage> messages = new UniqList<WtMessage>();
	public List<WtOperation> operations = new UniqList<WtOperation>();
	public List<WtPort> ports = new UniqList<WtPort>();
	public List<WtPolicy> policies = new UniqList<WtPolicy>();
}
